<?php

namespace App\Admin\Controllers;

use App\Models\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use Encore\Admin\Layout\Content;
use App\Admin\Extensions\CheckPartner;

class UserPartnerController extends Controller
{
    use HasResourceActions;

    //用户首页
    public function index(Content $content)
    {
        return Admin::content(function (Content $content) {
            $content->header('用户身份审核列表');
            $content->body($this->grid());
        });
    }

    //获取用户
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {
            $grid->column('id', 'ID')->sortable();
            $grid->column('name', '名称');
            $grid->column('phone', '注册手机号');
            $grid->column('id_card', '身份证号');
            $grid->column('id_card_positive', '身份证正面')->display(function ($id_card_positive) {
                if(!empty($id_card_positive)) {
                    return "<img style='width: 100px;height: 100px;' src='".$id_card_positive."' />";
                }else{
                    return '';
                }
            });
            $grid->column('id_card_contrary', '身份证反面')->display(function ($id_card_contrary) {
                if(!empty($id_card_contrary)) {
                    return "<img style='width: 100px;height: 100px;' src='".$id_card_contrary."' />";
                }else{
                    return '';
                }
            });
            $grid->column('partner_name', '通知姓名');
            $grid->column('partner_phone', '通知手机号');
            $grid->column('partner_address', '通知详细地址');
            $grid->column('identity', '申请身份')->using(['1' => '供应商加盟', '2' => '品牌店加盟', '3' => '代理商加盟']);
            $grid->column('created_at', '注册时间');

            $grid->disableCreateButton();

            $grid->disableRowSelector();

            $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->disableView();

                // append一个操作
                $actions->append(new CheckPartner($actions->getKey()));
            });

            $grid->tools(function ($tools) {
                // 禁用批量删除按钮
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });

            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();

                // 在这里添加字段过滤器
                $filter->column(1/3, function ($filter) {
                    $filter->like('name', '用户名');
                });
                $filter->column(1/3, function ($filter) {
                    $filter->like('phone', '注册手机');
                });
                $filter->column(1/3, function ($filter) {
                    $filter->like('id_card', '身份证号');
                });
            });

            $grid->model()->where('is_partner', '1');

            $grid->model()->orderBy('id', 'desc');

            //禁用导出按钮
            $grid->disableExport();
        });
    }


    //审核
    public function partner(Request $request)
    {
        $id = $request->post('id');
        $status = $request->post('status');

        if($status == 1) {
            $update['is_partner'] = '2';
        }else{
            $update['is_partner'] = '0';
            $update['identity'] = NULL;
        }

        $res = User::where('id', $id)->update($update);

        if(!$res) {
            return ['code' => 'false', 'msg' => '修改失败'];
        }

        return ['code' => 'true', 'msg' => '修改成功'];
    }
}
